﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using VitCode.Castle.DataAccess;

namespace CRS.Web.BaseData
{
    public partial class ManifestEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            
            pgList.DataSourceDelegate=new VitCode.Web.UI.PaginationGrid.GridDataSourceDelegate(BindData);
            if(!IsPostBack)
            {
                Session["ManfDataSource"] = null;
                BindDrpList();
                InitInfo();
                Session["SelectedManifest"] = null;
                pgList.BindGrid();
            }
        }

        private void InitInfo()
        {
            if(Session["SelectedManifest"]!=null)
            {
                var selectInfo = (DataRow[]) Session["SelectedManifest"];
                lblBillNo.Text = selectInfo[0]["单号"].ToString();
                txtDistributionCo.Text = selectInfo[0]["配货公司"].ToString();
                ctlDate.Text = selectInfo[0]["配货时间"].ToString();
                lblCreateUser.Text = selectInfo[0]["创建人"].ToString();
                lblCreateTime.Text = selectInfo[0]["创建时间"].ToString();
                drpList.SelectedItem.Text = selectInfo[0]["状态"].ToString();
                txtMemo.Text = selectInfo[0]["备注"].ToString();
            }
            else
            {
                lblBillNo.Text = "VIC_" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +
                                 DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() +
                                 DateTime.Now.Minute.ToString();
                lblCreateUser.Text = Server.UrlDecode(GenericContext.CurrUserName);
                lblCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

        protected void tabMenu_MenuItemClick(object sender, MenuEventArgs e)
        {
            if(tabMenu.SelectedValue=="0")
            {
                BillInfo.Visible = true;
                GoodsInfoDiv.Visible = false;
            }
            else
            {
                BillInfo.Visible = false;
                GoodsInfoDiv.Visible = true;
            }
        }

        private DataTable _MdataSource;

        private DataTable DataSource
        {
            get
            {
                if (Session["ManfDataSource"] != null)
                    _MdataSource = (DataTable)Session["ManfDataSource"];
                else
                {
                    _MdataSource = InitData();
                    Session["ManfDataSource"] = _MdataSource;
                }
                return _MdataSource;
            }
            set { Session["ManfDataSource"] = value; }
        }

        private object BindData()
        {
            return DataSource;
        }

        private DataTable InitData()
        {
            var table = new DataTable();
            table.Columns.Add("条码");
            table.Columns.Add("品名");
            table.Columns.Add("规格");
            table.Columns.Add("采购数量");
            table.Columns.Add("到货数量");
            table.Columns.Add("单位");
            table.Columns.Add("单价");
            table.Columns.Add("小计金额");
            for(var i=0;i<10;i++)
            {
                table.Rows.Add(new object[] {"A00000"+i,"B0000"+i,"C0000"+i,i,i,"个",i*100.00,i*i*100.00});
            }
            return table;
        }

        private void BindDrpList()
        {
            drpList.Items.Add("新建");
            drpList.Items.Add("理货中");
            drpList.Items.Add("已完成");
        }
    }
}
